package com.hospital.controller;

import com.hospital.dao.JurisdictionMapper;
import com.hospital.entity.*;
import com.hospital.service.UserDepartmentService;
import com.hospital.service.UserRoleService;
import com.hospital.util.JsonUtil;
import com.hospital.util.PagingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户对应角色与权限controller
 */
@Controller
@CrossOrigin
@RequestMapping("UserRole")
public class UserRoleController {
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private JsonUtil jsonUtil;
    @Autowired
    private JurisdictionMapper jurisdictionMapper;
    @Autowired
    private UserDepartmentService userDepartmentService;

    /**
     * 查所有用户对应的所有权限
     *
     * @param page    当前页
     * @param pageNum 每页显示条数
     * @return 集合中用户对应的角色与权限
     */
    @RequestMapping("SelectAll")
    @ResponseBody
    public List<Object> SelectAll(Integer page, Integer pageNum) {
        //集合
        List<Object> list = new ArrayList<>();
        //查所有数量
        Integer count = userRoleService.SelectAllCount();
        //分页工具类
        Map<String, Integer> paging = PagingUtil.paging(page, pageNum, count);
        Integer pages = paging.get("page");
        Integer pageNums = paging.get("pageNum");
        //分页查询
        List<User> users = userRoleService.SelectAll(pages, pageNums);
        //添加数据
        list.add(users);
        list.add(paging);
        //设置数据
        jsonUtil.setResults(list);
        return jsonUtil.getResults();
    }

    /**
     * 条件查询
     *
     * @param page        当前页
     * @param pageNum     每页显示条数
     * @param roleName    角色名称
     * @param roleRemarks 角色备注
     * @return 集合中的条件查询对象
     */
    @RequestMapping("SelectByUserRoleLike")
    @ResponseBody
    public List<Object> SelectByUserRoleLike(Integer page, Integer pageNum, String roleName, String roleRemarks) {
        //集合
        List<Object> list = new ArrayList<>();
        //条件查询数量
        Integer count = userRoleService.SelectByUserRoleLikeCount("%" + roleName + "%", "%" + roleRemarks + "%");
        //分页工具类
        Map<String, Integer> paging = PagingUtil.paging(page, pageNum, count);
        Integer pages = paging.get("page");
        Integer pageNums = paging.get("pageNum");
        //条件查询
        List<User> users = userRoleService.SelectByUserRoleLike(pages, pageNums, "%" + roleName + "%", "%" + roleRemarks + "%");
        //查询角色对应权限
        List<RoleJurisdictionVo> roleJurisdictionVos = jurisdictionMapper.SelectRoleJurisdiction();
        //遍历查询出来的用户
        for (User user : users) {
            //获取用户对应的角色信息
            List<Role> roles = user.getRoles();
            //遍历角色对应权限信息
            for (RoleJurisdictionVo roleJurisdictionVo : roleJurisdictionVos) {
                //遍历角色信息
                for (Role role : roles) {
                    //判断角色信息与角色权限id相同进行设置操作
                    if (role.getId().equals(roleJurisdictionVo.getRoleId())) {
                        //权限对象
                        Jurisdiction jurisdiction = new Jurisdiction(roleJurisdictionVo.getJId(), roleJurisdictionVo.getJurisdictionName(), roleJurisdictionVo.getJurisdictionRange());
                        List<Jurisdiction> jurisdictions = role.getJurisdictions();
                        jurisdictions.add(jurisdiction);
                        //设置数据进入权限集合
                        role.setJurisdictions(jurisdictions);
                    }
                }
            }
        }
        //添加数据
        list.add(users);
        list.add(paging);
        //设置数据
        jsonUtil.setResults(list);
        return jsonUtil.getResults();
    }

    /**
     * 添加角色方法
     *
     * @param userId 用户id
     * @param roleId 角色id
     * @return 添加状态
     */
    @ResponseBody
    @RequestMapping("Insert")
    public Boolean Insert(Integer userId, Integer roleId) {
        Integer state = userRoleService.Insert(userId, roleId);
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }

    /**
     * 修改角色方法
     *
     * @param userId 用户id
     * @param roleId 角色id
     * @return 修改状态
     */
    @ResponseBody
    @RequestMapping("Update")
    public Boolean Update(Integer userId, Integer roleId) {
        Integer state = userRoleService.Update(roleId, userId);
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }

    /**
     * 修改角色方法
     *
     * @param userId 用户id
     * @param roleId 角色id
     * @return 修改状态
     */
    @ResponseBody
    @RequestMapping("Updates")
    public Boolean Updates(Integer userId, @RequestParam("roleId[]") Integer[] roleId) {
        Integer state = 0;
        for (int i = 0; i < roleId.length; i++) {
            state = userRoleService.Delete(userId);
        }
        for (int i = 0; i < roleId.length; i++) {
            state = userRoleService.Insert(userId, roleId[i]);
        }
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }
    /**
     * 用户对应角色
     *
     * @return 集合中的用户对应角色信息
     */
    @ResponseBody
    @RequestMapping("SelectByUserRole")
    public List<Object> SelectByUserRole(Integer page, Integer pageNum) {
        List<Object> list = new ArrayList<>();
        Integer count = userRoleService.SelectByUserRoleCount();
        Map<String, Integer> paging = PagingUtil.paging(page, pageNum, count);
        Integer pages = paging.get("page");
        Integer pageNums = paging.get("pageNum");
        List<User> users = userRoleService.SelectByUserRole(pages, pageNums);
        //查询所有科室
        List<UserDepartment> userDepartments = userDepartmentService.SelectAllDepartment();
        //遍历所有用户
        for (User user : users) {
            //遍历所有用户对应的科室
            for (UserDepartment userDepartment : userDepartments) {
                if (user.getId().equals(userDepartment.getUserId())) {
                    //封装用户对应的科室
                    user.setResponsibleDepartment(userDepartment.getResponsibleDepartments());
                }
            }
        }
        list.add(users);
        list.add(paging);
        return list;
    }

    /**
     * 条件查询
     *
     * @param responsibleDepartment 科室名称
     * @param name                  人员名称
     * @return 集合中的用户与角色信息
     */
    @ResponseBody
    @RequestMapping("SelectUserRoleLike")
    public List<Object> SelectUserRoleLike(Integer page, Integer pageNum, String responsibleDepartment, String name) {
        System.out.println(page + "," + pageNum);
        List<Object> list = new ArrayList<>();
        Integer count = userRoleService.SelectUserRoleLikeCount(responsibleDepartment, "%" + name + "%");
        Map<String, Integer> paging = PagingUtil.paging(page, pageNum, count);
        Integer pages = paging.get("page");
        Integer pageNums = paging.get("pageNum");
        List<User> users = userRoleService.SelectUserRoleLike(pages, pageNums, responsibleDepartment, "%" + name + "%");
        //查询所有科室
        List<UserDepartment> userDepartments = userDepartmentService.SelectAllDepartment();
        //遍历所有用户
        for (User user : users) {
            //遍历所有用户对应的科室
            for (UserDepartment userDepartment : userDepartments) {
                if (user.getId().equals(userDepartment.getUserId())) {
                    //封装用户对应的科室
                    user.setResponsibleDepartment(userDepartment.getResponsibleDepartments());
                }
            }
        }
        list.add(users);
        list.add(paging);
        return list;
    }


}
